package org.jeecg.modules.contract.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.docx4j.Docx4J;
import org.docx4j.convert.out.FOSettings;
import org.docx4j.fonts.IdentityPlusMapper;
import org.docx4j.fonts.Mapper;
import org.docx4j.fonts.PhysicalFonts;
import org.docx4j.openpackaging.exceptions.Docx4JException;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.contract.entity.ContractFile;
import org.jeecg.modules.contract.entity.ContractFileEnum;
import org.jeecg.modules.contract.mapper.ContractFileMapper;
import org.jeecg.modules.contract.service.IContractFileService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.time.LocalTime;
import java.util.UUID;

/**
 * @Description: 文件上传
 * @Author: jeecg-boot
 * @Date: 2019-07-29
 * @Version: V1.0
 */
@Service
public class ContractFileServiceImpl extends ServiceImpl <ContractFileMapper, ContractFile> implements IContractFileService {

    @Value("${jeecg.path.upload}")
    private String uploadPath;


    /**
     * 保存后端
     *
     * @param byId
     * @param request
     */
    @Override
    public String savePDF(ContractFile byId, HttpServletRequest request) {

        // 读work进来
        File workFile=new File(byId.getFilePath());
        WordprocessingMLPackage load=null;
        try {
            // 转换work
            load=WordprocessingMLPackage.load(workFile);
            // 常用文件转码
            load.setFontMapper(setMapper());
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 项目路径
        //ContractFileEnum.PDF_FILE.file()
        // 写进pdf.js文件夹
        try {
            String pdfName=byId.getFileName().replaceAll("docx", "pdf");
            if (!new File(uploadPath + "/" + ContractFileEnum.PDF_FILE.getFile()).exists()) {
                new File(uploadPath + "/" + ContractFileEnum.PDF_FILE.getFile()).mkdirs();
            }
            String tempName=String.valueOf(DateUtils.getMillis()) + pdfName;
            FileOutputStream fileOutputStream=new FileOutputStream(uploadPath + "/" + ContractFileEnum.PDF_FILE.getFile() + "/" + tempName);
            FOSettings foSettings=Docx4J.createFOSettings();
            foSettings.setWmlPackage(load);
            Docx4J.toFO(foSettings, fileOutputStream, Docx4J.FLAG_EXPORT_PREFER_XSL);
            String id=UUID.randomUUID().toString();
            ContractFile contractFile=new ContractFile();
            contractFile.setFileSize("0");
            contractFile.setFileName(pdfName);
            contractFile.setFileSaveName(tempName);
            contractFile.setFilePath(uploadPath + "/" + ContractFileEnum.PDF_FILE.getFile() + "/" + tempName);
            contractFile.setPkFile(id);
            this.save(contractFile);
            return id;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (Docx4JException e) {
            e.printStackTrace();
        }

        return "";
    }

    /**
     * 常用样式
     *
     * @return
     */
    private Mapper setMapper() {
        Mapper fontMapper=new IdentityPlusMapper();
        fontMapper.put("隶书", PhysicalFonts.get("LiSu"));
        fontMapper.put("宋体", PhysicalFonts.get("SimSun"));
        fontMapper.put("微软雅黑", PhysicalFonts.get("Microsoft Yahei"));
        fontMapper.put("黑体", PhysicalFonts.get("SimHei"));
        fontMapper.put("楷体", PhysicalFonts.get("KaiTi"));
        fontMapper.put("新宋体", PhysicalFonts.get("NSimSun"));
        fontMapper.put("华文行楷", PhysicalFonts.get("STXingkai"));
        fontMapper.put("华文仿宋", PhysicalFonts.get("STFangsong"));
        fontMapper.put("宋体扩展", PhysicalFonts.get("simsun-extB"));
        fontMapper.put("仿宋", PhysicalFonts.get("FangSong"));
        fontMapper.put("仿宋_GB2312", PhysicalFonts.get("FangSong_GB2312"));
        fontMapper.put("幼圆", PhysicalFonts.get("YouYuan"));
        fontMapper.put("华文宋体", PhysicalFonts.get("STSong"));
        fontMapper.put("华文中宋", PhysicalFonts.get("STZhongsong"));
        return fontMapper;
    }
}
